//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System.Collections.Generic;

namespace GuidanceExplorer.Model
{
    /// <summary>
    /// A collection of Columns that has unique names.
    /// </summary>
    public class ColumnCollection : Dictionary<string, Column>
    {
        #region Static members

        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static ColumnCollection New()
        {
            return new ColumnCollection();
        }

        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static ColumnCollection DefaultColumns( GuidanceType guidanceType )
        {
            ColumnCollection columns = new ColumnCollection();

            foreach (Field field in guidanceType.Fields.Values)
            {
                Column column = Column.New();
                column.Source = field.Name;
                column.Caption = field.Caption;
                column.Sort = field.DefaultSort;
                column.Visible = true;
                column.Width = field.Width;
                column.Field = field;
                column.Position = field.Position;
                column.Visible = field.Visible;

                columns.Add(column.Source, column);
            }

            return columns;
        }

        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static ColumnCollection DefaultGuidanceTypeColumns(GuidanceType guidanceType)
        {
            ColumnCollection columns = new ColumnCollection();

            foreach (Field field in guidanceType.Fields.Values)
            {
                GuidanceTypeColumn column = GuidanceTypeColumn.New();
                column.Source = field.Name;
                column.Caption = field.Caption;
                column.Sort = field.DefaultSort;
                column.Visible = true;
                column.Width = field.Width;
                column.Field = field;
                column.Position = field.Position;
                column.Visible = field.Visible;

                columns.Add(column.Source, column);
            }

            return columns;
        }

        #endregion

        /// <summary>
        /// Default private constructor.
        /// </summary>
        private ColumnCollection() { }

        public override bool Equals(object obj)
        {
            ColumnCollection other = obj as ColumnCollection;
            if (other != null)
            {
                if (this.Count != other.Count) return false;
                IEnumerator<Column> _thisCollection = this.Values.GetEnumerator();
                IEnumerator<Column> _otherCollection = other.Values.GetEnumerator();
                while (_thisCollection.MoveNext() && _otherCollection.MoveNext())
                {
                    if (!_thisCollection.Current.Equals(_otherCollection.Current))
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return base.Equals(obj);
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}

